package com.htx.oldbookpavilion.service.mycenter.impl;

import com.htx.oldbookpavilion.common.StandardPageData;
import com.htx.oldbookpavilion.entity.BookInfo;
import com.htx.oldbookpavilion.entity.BookInfoExample;
import com.htx.oldbookpavilion.entity.ShoppingList;
import com.htx.oldbookpavilion.entity.ShoppingListExample;
import com.htx.oldbookpavilion.mapper.ShoppingListExtMapper;
import com.htx.oldbookpavilion.service.mycenter.ShoppingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class ShoppingListServiceImpl implements ShoppingListService {

    @Autowired
    private ShoppingListExtMapper shoppingListExtMapper;

    /**
     * 分页获取购书清单
     * @param sPageData
     */
    @Override
    public void getShoppingListByPage(StandardPageData<ShoppingList> sPageData) {
        if (StringUtils.isEmpty(sPageData.getSearchParam().getUserInfoId())) {
            return ;
        }
        List<ShoppingList> shoppingLists = shoppingListExtMapper.queryShoppingListByPage(sPageData);
        long total = shoppingListExtMapper.countByStandardPage(sPageData);
        sPageData.setTotal(total);
        sPageData.setRecords(shoppingLists);
    }

    /**
     * 根据id删除购书清单数据
     * @param shoppingLists
     * @return
     */
    @Override
    public boolean deleteShoppingList(List<ShoppingList> shoppingLists) {
        int count = 0;
        if(shoppingLists.size() == 1){
            //删除单条记录
            count = shoppingListExtMapper.deleteByPrimaryKey(shoppingLists.get(0).getShoppingListId());
        }else{
            //批量删除多条记录
            ShoppingListExample shoppingListExample = new ShoppingListExample();
            for (ShoppingList shoppingList : shoppingLists) {
                shoppingListExample.or().andShoppingListIdEqualTo(shoppingList.getShoppingListId());
            }
            count = shoppingListExtMapper.deleteByExample(shoppingListExample);
        }
        return count != 0;
    }

    /**
     * 更新购书清单数据
     * @param shoppingLists
     * @return
     */
    @Override
    public boolean updateShoppingList(List<ShoppingList> shoppingLists) {
        int count = 0;
        //添加最后修改时间
        for (ShoppingList shoppingList : shoppingLists) {
            shoppingList.setLastModifiedTime(new Date());
            int i = shoppingListExtMapper.updateByPrimaryKeySelective(shoppingList);
            count += i;
        }
        return count != 0;
    }
}
